package com.example.attack.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.attack.mapper.ElCharacterMapper;
import com.example.attack.model.ElCharacter;
import com.example.attack.service.TransactionAService;
import com.example.attack.service.TransactionBService;
import com.example.attack.service.TransactionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.transaction.reactive.TransactionSynchronizationManager;

@Service
public class TransactionServiceImpl extends ServiceImpl<ElCharacterMapper, ElCharacter> implements TransactionService {

    @Autowired
    private TransactionAService transactionAService;
    @Autowired
    private TransactionBService transactionBService;

    @Override
    @Transactional
    public void forIndependentTransaction() {
        for (int i = 0; i < 3; i++) {

            try {
                // 第三次抛出异常
                transactionAService.createElCharacter(i);
            } catch (Exception e) {
                System.out.println(e.getLocalizedMessage());
            }

        }
    }

    @Transactional
    public void createElCharacter(int i) {
        ElCharacter elCharacter = ElCharacter.builder().name("transaction").atk(new Double(i)).build();
        this.save(elCharacter);
        if(i == 2){
            throw new RuntimeException("第三次出错");
        }
    }

}
